package database;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

public class Fragment {
	private String name;
	private String process;
	private String position;
	
	private HashMap<String, ArrayList<Operation>> relationshipList;
	private HashMap<String, ArrayList<Operation>> elementList;
	
	
	public Fragment(String _name, String _process, String _position){
		name=_name;
		process=_process;
		position=_position;
		relationshipList= new HashMap<String, ArrayList<Operation>>();
		elementList= new HashMap<String, ArrayList<Operation>>();
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getProcess() {
		return process;
	}

	public void setProcess(String process) {
		this.process = process;
	}

	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	

	public HashMap<String, ArrayList<Operation>> getElementList() {
		return elementList;
	}
	
	public void addElement(String nameComp, ArrayList<Operation> ops){
		elementList.put(nameComp, ops);
	}
	
	public HashMap<String, ArrayList<Operation>> getRelationshipList() {
		return relationshipList;
	}
	
	public void addRelationship(String nameComp, ArrayList<Operation> ops){
		relationshipList.put(nameComp, ops);
	}
	
	public ArrayList<String> getComponentNames(){
		ArrayList<String> names= new ArrayList<String>();
		Set cles = elementList.keySet();
		Iterator it = cles.iterator();
		while (it.hasNext()){
		   String key = (String) it.next(); 
		   names.add(key);
		}
		cles = relationshipList.keySet();
		it = cles.iterator();
		while (it.hasNext()){
			String key = (String) it.next(); 
			names.add(key);
		}
		return names;
	}
	
	public void addOperation(String comp, Operation op){
		Set cles = elementList.keySet();
		Iterator it = cles.iterator();
		while (it.hasNext()){
		   String cle = (String) it.next(); 
		   if(cle.equals(comp)){
			   elementList.get(cle).add(op);
		   }
		}
		cles = relationshipList.keySet();
		it = cles.iterator();
		while (it.hasNext()){
		   String cle = (String) it.next(); 
		   if(cle.equals(comp)){
			   relationshipList.get(cle).add(op);
		   }
		}
	}
	
	public String toString(){
		String fgt=" FGT : "+ this.getName() +" ( process : "+ this.process+ " , position : "+getPosition()+" ) \n";
		Set cles = elementList.keySet();
		Iterator it = cles.iterator();
		while (it.hasNext()){
		   String cle = (String) it.next(); 
		   fgt=fgt+"\t"+cle+" \t ";
		   ArrayList<Operation> valeur = elementList.get(cle); 
		   for (int i=0;i<valeur.size();i++)
			   fgt=fgt+" "+ valeur.get(i);
		   fgt=fgt+"\n";
		}
		cles = relationshipList.keySet();
		it = cles.iterator();
		while (it.hasNext()){
		   String cle = (String) it.next(); 
		   fgt=fgt+"\t"+cle+" \t ";
		   ArrayList<Operation> valeur = relationshipList.get(cle); 
		   for (int i=0;i<valeur.size();i++)
			   fgt=fgt+" "+ valeur.get(i);
		   fgt=fgt+"\n";
		}
		return fgt;
	}
}
